<?php

/**
 * administracion actions.
 *
 * @package    sgcj
 * @subpackage administracion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class administracionActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  
  public function executeIndex()
  {
  }
  
  public function executeIndextribunal()
  {
  }
  
  public function executeListtribunal()
  {
    $this->tribunalList = TribunalPeer::doSelect(new Criteria());
  }
  
  public function executeSearchtribunal($request)
  {
  	$this->accion = $request->getParameter('accion');
  	$this->form = new SearchTribunalForm();
  	$this->form->setDefault('accion',$request->getParameter('accion'));
  }
  
  public function executeSearchtribunalupdate($request)
  {
  	$this->tribunalList = TribunalPeer::doSelect(TribunalPeer::getCriteria($request));
  	$this->accion = $request->getParameter('accion');
	$this->setTemplate('listtribunal');
  }
  public function executeShowtribunal($request)
  {
    $this->tribunal = TribunalPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->tribunal);
  }
  
  public function executeCreatetribunal()
  {
    $this->form = new TribunalForm();

    $this->setTemplate('edittribunal');
  }
  
  public function executeEdittribunal($request)
  {
    $this->form = new TribunalForm(TribunalPeer::retrieveByPk($request->getParameter('id')));
  }
  
  public function executeUpdatetribunal($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new TribunalForm(TribunalPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('tribunal'));
    if ($this->form->isValid())
    {
      $this->tribunal = $this->form->save();
      $this->setTemplate('showtribunal');
    }else{
      $this->setTemplate('edittribunal');
    }
  }

  public function executeDeletetribunal($request)
  {
    $this->forward404Unless($tribunal = TribunalPeer::retrieveByPk($request->getParameter('id')));

    $tribunal->delete();

    $this->redirect('administracion/indextribunal');
  }
  
  public function executeIndexacreedor()
  {
  }
  
  public function executeListacreedor()
  {
    $this->acreedorList = AcreedorPeer::doSelect(new Criteria());
  }
  
  public function executeSearchacreedor($request)
  {
  	$this->accion = $request->getParameter('accion');
  	$this->form = new SearchAcreedorForm();
  	$this->form->setDefault('accion',$request->getParameter('accion'));
  }
  
  public function executeSearchacreedorupdate($request)
  {
  	$this->acreedorList = AcreedorPeer::doSelect(AcreedorPeer::getCriteria($request));
  	$this->accion = $request->getParameter('accion');
	$this->setTemplate('listacreedor');
  }
  
  public function executeShowacreedor($request)
  {
    $this->acreedor = AcreedorPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->acreedor);
  }
  
  public function executeCreateacreedor($request)
  {
    $this->form = new AcreedorForm();
    $this->setTemplate('editacreedor');
    $this->pathinfo = $request->getPathInfo();
  }
  
  public function executeEditacreedor($request)
  {
    $this->form = new AcreedorForm(AcreedorPeer::retrieveByPk($request->getParameter('id')));
    $this->pathinfo = $request->getPathInfo();
  }
  
  public function executeUpdateacreedor($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new AcreedorForm(AcreedorPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('acreedor'));
    if ($this->form->isValid())
    {
      $this->acreedor = $this->form->save();
      $this->setTemplate('showacreedor');
    }else{
      $this->setTemplate('editacreedor');
    }
  }

  public function executeDeleteacreedor($request)
  {
    $this->forward404Unless($acreedor = AcreedorPeer::retrieveByPk($request->getParameter('id')));

    $acreedor->delete();

    $this->redirect('administracion/indexacreedor');
  }
  
  public function executeIndexdeudor()
  {
  }
  public function executeListdeudor()
  {
    $this->deudorList = DeudorPeer::doSelect(new Criteria());
  }
  
  public function executeSearchdeudor($request)
  {
  	$this->accion = $request->getParameter('accion');
  	$this->form = new SearchDeudorForm();
  	$this->form->setDefault('accion',$request->getParameter('accion'));
  }
  
  public function executeSearchdeudorupdate($request)
  {
  	$this->deudorList = DeudorPeer::doSelect(DeudorPeer::getCriteria($request));
  	$this->accion = $request->getParameter('accion');
	$this->setTemplate('listdeudor');
  }

  public function executeShowdeudor($request)
  {
    $this->deudor = DeudorPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->deudor);
  }

  public function executeCreatedeudor($request)
  {
    $this->form = new DeudorForm();
    $this->setTemplate('editdeudor');
    $this->pathinfo = $request->getPathInfo();
  }

  public function executeEditdeudor($request)
  {
    $this->form = new DeudorForm(DeudorPeer::retrieveByPk($request->getParameter('id')));
    $this->pathinfo = $request->getPathInfo();
  }

  public function executeUpdatedeudor($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new DeudorForm(DeudorPeer::retrieveByPk($request->getParameter('id')));
	$this->pathinfo = $request->getPathInfo();
    $this->form->bind($request->getParameter('deudor'));
    if ($this->form->isValid())
    {
      $this->deudor = $this->form->save();
	  $this->setTemplate('showdeudor');
    }else{
      $this->setTemplate('editdeudor');
    }
  }

  public function executeDeletedeudor($request)
  {
    $this->forward404Unless($deudor = DeudorPeer::retrieveByPk($request->getParameter('id')));

    $deudor->delete();

    $this->redirect('administracion/indexdeudor');
  }
  public function executeIndexdeudordomicilio()
  {
  }
  
  public function executeSearchdeudordomicilioupdate($request)
  {
  	$this->deudor_domicilioList = DeudorDomicilioPeer::getByIdDeudor($request->getParameter('iddeudor'));
  	$this->accion = $request->getParameter('accion');
	$this->setTemplate('listdeudordomicilio');
  }
  
  public function executeShowdeudordomicilio($request)
  {
    $this->deudor_domicilio = DeudorDomicilioPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->deudor_domicilio);
  }

  public function executeCreatedeudordomicilio()
  {
    $this->form = new DeudorDomicilioForm();

    $this->setTemplate('editdeudordomicilio');
  }

  public function executeEditdeudordomicilio($request)
  {
    $this->form = new DeudorDomicilioForm(DeudorDomicilioPeer::retrieveByPK($request->getParameter('iddeudordomicilio')));
    $criteria = new Criteria();
    $criteria->add(DeudorPeer::ID, $request->getParameter('iddeudor'));
    $this->form->setDefault('deudor_id', DeudorPeer::doSelectOne($criteria)->getId());
    $this->deudor = DeudorPeer::doSelectOne($criteria)->__toString();
  }

  public function executeUpdatedeudordomicilio($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new DeudorDomicilioForm(DeudorDomicilioPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('deudor_domicilio'));
    if ($this->form->isValid())
    {
    $this->deudor_domicilio = $this->form->save();
	$this->setTemplate('showdeudordomicilio');
    }else{
      $this->setTemplate('editdeudordomicilio');
    }
  }

  public function executeDeletedeudordomicilio($request)
  {
    $this->forward404Unless($deudor_domicilio = DeudorDomicilioPeer::retrieveByPk($request->getParameter('id')));

    $deudor_domicilio->delete();

    $this->redirect('administracion/indexdeudor');
  }
  public function executeListcontrato()
  {
  	$this->contratoList = ContratoPeer::doSelect(new Criteria());
  }
  
	public function executeShowcontrato($request)
  {
  	$criteria = new Criteria();
  	$criteria->add(ContratoPeer::ACREEDOR_ID, $request->getParameter('idacreedor'));
  	$criteria->add(ContratoPeer::ESTADO, true);
    $this->contrato = ContratoPeer::doSelectOne($criteria);
    $this->forward404Unless($this->contrato);
    $this->acreedor = AcreedorPeer::retrieveByPK($this->contrato->getAcreedorId());
  }
  
	public function executeCreatecontrato()
  {
    $this->form = new ContratoForm();

    $this->setTemplate('editcontrato');
  }

  public function executeEditcontrato($request)
  {
  	$this->form = new ContratoForm();
    $criteria = new Criteria();
    $criteria->add(AcreedorPeer::ID, $request->getParameter('idacreedor'));
    $this->form->setDefault('acreedor_id', AcreedorPeer::doSelectOne($criteria)->getId());
    $this->form->setDefault('estado', true);
    $this->form->setDefault('fecha_inicio',strtotime("now"));
    $this->acreedorName = AcreedorPeer::doSelectOne($criteria);
  }

  public function executeUpdatecontrato($request)
  {
    $this->forward404Unless($request->isMethod('post'));
    $this->form = new ContratoForm(ContratoPeer::retrieveByPk($request->getParameter('id')));
    $this->form->bind($request->getParameter('contrato'));
    if ($this->form->isValid())
    {
     $acreedor = AcreedorPeer::retrieveByPK($this->form->getValue('acreedor_id'));
      if($acreedor->tieneContratoActivo()){
      	$contrato = $acreedor->getContratoActivo();
      	$contrato->setEstado(false);
      	$contrato->setFechaTermino("now");
      	$contrato->save();
      }
      $this->contrato = $this->form->save();
      $this->setTemplate('showcontrato');
    }else{
      $this->acreedorName = $request->getParameter('acreedorName');
      $this->setTemplate('editcontrato');	
    }
  }

  public function executeDesactivarcontrato($request)
  {
    $criteria = new Criteria();
  	$criteria->add(ContratoPeer::ACREEDOR_ID, $request->getParameter('idacreedor'));
  	$criteria->add(ContratoPeer::ESTADO, true);
    $this->contrato = ContratoPeer::doSelectOne($criteria);
    $this->contrato->setEstado(false);
	$this->contrato->save();
  }
  
}
